<template>
  <div class="head-container">
    <!-- 搜索 -->
    <div v-show="sup_this.searchToggle">
      <el-select v-model="paramBean.processStatus" size="mini" style="width: 140px;" clearable placeholder="请选择处理状态">
        <el-option v-for="item in dicts.qc_process_status" :key="item.id" :label="item.dictName" :value="item.dictCode"/>
      </el-select>
      <el-input v-model="paramBean.busOtherCpyName" clearable placeholder="输入供应商搜索" style="width: 200px;" class="filter-item" @keyup.enter.native="sup_this.toQuery"/>
      <el-input v-model="paramBean.sourceCode" clearable placeholder="输入单号搜索" style="width: 200px;" class="filter-item" @keyup.enter.native="sup_this.toQuery"/>
      <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="sup_this.toQuery">搜索</el-button>
      <el-button class="filter-item" size="mini" type="warning" icon="el-icon-refresh-left" @click="sup_this.resetQuery()">重置</el-button>
    </div>
    <div class="crud-opts">
      <el-button
        v-permission="['SYS_ADMIN','COMMON_ADMIN','QC_CHECK_ALL','QC_CHECK_CREATE','QC_CHECK_CHANGE']"
        class="filter-item"
        size="mini"
        type="primary"
        icon="el-icon-plus"
        @click="toQcCheck">生成检验单</el-button>
      <el-button
        v-permission="['SYS_ADMIN','COMMON_ADMIN','QC_CHECK_ALL','QC_CHECK_CREATE','QC_CHECK_CHANGE']"
        class="filter-item"
        size="mini"
        type="warning"
        icon="el-icon-finished"
        @click="toManualDialog">标识已检验</el-button>
      <DataGridOpts :sup_this="sup_this" @refreshClick="refreshClick" @searchClick="searchClick" />
    </div>
    <qc-form ref="qcForm" :sup_this="sup_this" />
    <el-dialog :visible.sync="manualDialog" size="mini" title="手工标识检验结果" append-to-body width="400px">
      <el-form label-width="100px">
        <el-form-item label="选择测试结果">
          <el-radio v-for="item in dicts.qc_result" v-model="qcCheckResult" :key="item.id" :label="item.dictCode">
            {{ item.dictName }}
          </el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="cancelDialog">取消</el-button>
        <el-button type="primary" @click="doManualProcessQc">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import qcWait from '@/api/prod/QcWait'
import QcForm from '@/views/prod/qc/module/form'

export default {
  components: { QcForm },
  dictTypes: ['qc_process_status', 'qc_result'],
  props: {
    paramBean: {
      type: Object,
      required: true
    },
    sup_this: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      buttonLoading: false,
      manualDialog: false,
      qcCheckResult: 'pass'
    }
  },
  methods: {
    refreshClick() {
      this.sup_this.init()
    },
    searchClick() {
      this.sup_this.toggleSearch()
    },
    toQcCheck() {
      const _this = this.sup_this
      const busRelList = []
      let repeatKey = null
      let diffFlag = false
      _this.selectedGridRows.forEach(row => {
        const tmpkey = row.otherSysOwnerCpy
        if (repeatKey && repeatKey !== tmpkey) {
          diffFlag = true
          return
        }
        repeatKey = tmpkey
        busRelList.push({ fromBusModule: row.busModule, fromBusClass: row.busClass, fromCode: row.sysGenCode, fromId: row.id })
      })
      if (diffFlag) {
        this.$message({
          message: '请选择相同供应商',
          type: 'warning'
        })
        return
      }
      if (busRelList.length === 0) {
        this.$message({
          message: '请选择记录',
          type: 'warning'
        })
        return
      }
      const _thisform = this.$refs.qcForm
      _thisform.initDataOfBefore()
      _thisform.addDataFromQcWait(busRelList)
    },
    toManualDialog() {
      const _this = this.sup_this
      const selectIds = []
      _this.selectedGridRows.forEach(row => {
        selectIds.push(row.id)
      })
      if (selectIds.length === 0) {
        this.$message({
          message: '请选择记录',
          type: 'warning'
        })
        return
      }
      this.manualDialog = true
    },
    cancelDialog() {
      this.manualDialog = false
    },
    doManualProcessQc() {
      const _this = this.sup_this
      const selectIds = []
      _this.selectedGridRows.forEach(row => {
        selectIds.push(row.id)
      })
      if (selectIds.length === 0) {
        this.$message({
          message: '请选择记录',
          type: 'warning'
        })
        return
      }
      this.buttonLoading = true
      this.$confirm('确认要处理?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        const data = { idList: selectIds, 'qcCheckResult': this.qcCheckResult }
        qcWait.doManualProcessQc(data).then(res => {
          this.buttonLoading = false
          this.cancelDialog()
          _this.init()
        }).catch(err => {
          this.buttonLoading = false
          console.log(err)
        })
      }).catch(() => {
        this.buttonLoading = false
      })
    }
  }
}
</script>
